package com.vhuomai.product.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Scope;
import com.vhuomai.product.dto.SpuItem;
import com.vhuomai.product.entity.BaseSpu;
import com.vhuomai.product.entity.BaseSpuItem;
import com.vhuomai.product.entity.BaseSpuItemOption;
import com.vhuomai.product.service.SpuService;
import com.vhuomai.system.constant.UserRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author harris
 */
@Api(tags = "SPU")
@RestController
@RequestMapping("/api")
@Scope(role = UserRole.MANAGER)
public class SpuController {

    @Autowired
    private SpuService spuService;

    @ApiOperation(value = "目录spu")
    @GetMapping(value = "/product/spu", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<List<BaseSpu>> spuList(@RequestParam("categoryId") int categoryId) {
        return ServiceResult.success(spuService.findByCategory(categoryId));
    }

    @ApiOperation(value = "添加spu")
    @PostMapping(value = "/product/spu", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<BaseSpu> addSpu(@RequestBody BaseSpu spu) {
        return ServiceResult.success(spuService.add(spu));
    }

    @ApiOperation(value = "更新spu")
    @PutMapping(value = "/product/spu", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> updateSpu(@RequestBody BaseSpu spu) {
        spuService.update(spu);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除spu")
    @DeleteMapping(value = "/product/spu", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> updateSpu(@RequestParam("id") int id) {
        spuService.remove(id);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "spu字段列表")
    @GetMapping(value = "/product/spu/items", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<List<SpuItem>> spuItems(@RequestParam("spuId") int spuId) {
            return ServiceResult.success(spuService.findItemsBySpuId(spuId));
    }

    @ApiOperation(value = "添加spu字段")
    @PostMapping(value = "/product/spu/item", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<BaseSpuItem> addItem(@RequestBody BaseSpuItem spuItem) {
        return ServiceResult.success(spuService.addSpuItem(spuItem));
    }

    @ApiOperation(value = "更新spu字段")
    @PutMapping(value = "/product/spu/item", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> updateItem(@RequestBody BaseSpuItem spuItem) {
        spuService.updateSpuItem(spuItem);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除spu字段")
    @DeleteMapping(value = "/product/spu/item", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> updateItem(@RequestParam("id") int id) {
        spuService.removeSpuItem(id);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "添加spu字段值")
    @PostMapping(value = "/product/spu/item/option", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<BaseSpuItemOption> addItemOption(@RequestBody BaseSpuItemOption spuItem) {
        return ServiceResult.success(spuService.addSpuItemOption(spuItem));
    }

    @ApiOperation(value = "更新spu字段值")
    @PutMapping(value = "/product/spu/item/option", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> updateItemOption(@RequestBody BaseSpuItemOption spuItemOption) {
        spuService.updateSpuItemOption(spuItemOption);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除spu字段值")
    @DeleteMapping(value = "/product/spu/item/option", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> updateItemOption(@RequestParam("id") int id) {
        spuService.removeSpuItemOption(id);
        return ServiceResult.SUCCESS;
    }
}
